package com.koala.myView;

import com.association.R;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class NavigationListView extends View {
	private OnSelectLetterChangedListener onSelectLetterChangedListener ;
	private Context mContext ;
	private int SELECTED = -1 ;
	private Paint mPaint = new Paint() ;
	private String[] navigationLetters = new String[]{"#","A","B","C","D","E","F","G","H","I",
			"J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
	private boolean showBackgound = false ;
	
	public NavigationListView(Context context){
		super(context) ;
		mContext = context ;
	}

	public NavigationListView(Context context , AttributeSet attr ){
		super(context , attr) ;
		mContext = context ;
	}

	public NavigationListView(Context context , AttributeSet attr , int defStyle ){
		super(context , attr , defStyle);
		mContext = context ;
	}

	@Override 
	protected  void onDraw(Canvas canvas){

		super.onDraw(canvas);
		if(showBackgound){
			canvas.drawColor(Color.TRANSPARENT) ;
		}
		int fatherHeight = getHeight();
		int fatherWidth = getWidth();
		int itemHeight = fatherHeight / navigationLetters.length ;
		int navigationTextSize =(int) mContext.getResources().getDimension(R.dimen.navigation_textsize);
		System.out.println("navigation_textsize = " + navigationTextSize);
		for(int i=0 ; i< navigationLetters.length ; i++){
			mPaint.setColor(Color.WHITE);
			mPaint.setAntiAlias(true) ;
			mPaint.setTextSize(navigationTextSize) ;
			if(i == SELECTED){  // if navigationLetters[i] was selected , draw the differnt clor 
				mPaint.setColor(Color.YELLOW);
			}else{
				mPaint.setColor(Color.BLACK);
			}
			float xPos = (fatherWidth - mPaint.measureText(navigationLetters[i])) / 2 ;
			float yPos = itemHeight * i + itemHeight  ;
			canvas.drawText(navigationLetters[i] , xPos ,yPos ,mPaint) ;
			mPaint.reset() ;
		}


	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event){
		int oldSelect =  SELECTED ;
		final int selctedLetterIndex =(int) (event.getY() / getHeight() * navigationLetters.length) ;
		
		System.out.println("navigation dispatchTouchEvent start : listener ==  " + onSelectLetterChangedListener);
		System.out.println("navigation dispatchTouchEvent start : seletedIndex ==  " + selctedLetterIndex);

		switch(event.getAction()){
		case MotionEvent.ACTION_DOWN :
			System.out.println("navigation dispatchTouchEvent action_down ");
			showBackgound  = true ;
			if(oldSelect != selctedLetterIndex && onSelectLetterChangedListener != null ){
				if(selctedLetterIndex >= 0 && selctedLetterIndex < navigationLetters.length){
					onSelectLetterChangedListener.onSelectLetterChanged(navigationLetters[selctedLetterIndex]);
					SELECTED = selctedLetterIndex ;
					invalidate();    // recall the onDraw method to redraw the view
					System.out.println("navigation dispatchTouchEvent action_down ,then  view invalidate  ");
				}
			}
			break ;
		case MotionEvent.ACTION_MOVE :
			if(oldSelect != selctedLetterIndex && onSelectLetterChangedListener != null){
				if(selctedLetterIndex >= 0 && selctedLetterIndex < navigationLetters.length){
					onSelectLetterChangedListener.onSelectLetterChanged(navigationLetters[selctedLetterIndex]);
					SELECTED = selctedLetterIndex ;
					invalidate();
					System.out.println("navigation dispatchTouchEvent action_move ");
				}
			}
			break ;
		case MotionEvent.ACTION_UP :
			showBackgound  = false  ;
			SELECTED = -1 ;  // restore the origin view 
			invalidate() ;		
			System.out.println("navigation dispatchTouchEvent action_up ");
			break ;
		default : break ;

		}

		return true  ;
	}

	@Override
	public  boolean  onTouchEvent(MotionEvent event ){
		return super.onTouchEvent(event);
	}
	
	/**
	 *  set navigation letters cliked listener
	 * @param onSelectLetterChangedListener navigation letters cliked listener
	 */
	public void setOnSelectLetterChangedListener(OnSelectLetterChangedListener onSelectLetterChangedListener){
		this.onSelectLetterChangedListener = onSelectLetterChangedListener ;
	}
	
	/**
	 * navigation letters clicked listener 
	 * @author forest
	 *
	 */
	public interface OnSelectLetterChangedListener{
		
		/**
		 * call back when user click  navigation letters 
		 * @param selectedKey the clicked letter
		 */
		public void onSelectLetterChanged(String selectedKey);
	}
}
